package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.query.SearchQueryObject;
import cn.wolfcode.trip.base.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("search")
public class SearchController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private INewsPageService newsPageService;

    /**
     * 根据关键字查询攻略/游记/日志的总数
     *
     * @param keyword
     * @return
     */
    @GetMapping("{keyword}/count")
    public Map getCount(@PathVariable("keyword") String keyword) {
        HashMap<String, Object> map = new HashMap<>();
        int strategyCount = strategyService.selectCountByKeyword(keyword);
        int travelCount = travelService.selectCountByKeyword(keyword);
        int newsCount = newsPageService.selectCountByKeyword(keyword);
        map.put("strategyCount", strategyCount);
        map.put("travelCount", travelCount);
        map.put("newsPageCount", newsCount);
        return map;
    }

    /**
     * 通过关键字和type查询攻略/游记/日报
     *
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo search(SearchQueryObject qo) {
        if (qo.getType() == 1) {
            return strategyService.searchQuery(qo);
        } else if (qo.getType() == 2) {
            return travelService.searchQuery(qo);
        } else if (qo.getType() == 3) {
            return newsPageService.searchQuery(qo);
        }
        return null;
    }
}